package com.jy.rhin.domain.document.validation;

import com.jy.api.upload.UploadAPI;
import com.jy.common.StringUtil;
import com.jy.rhin.infra.repo.document.validation.po.DocumentValidationFilePo;
import com.jy.rhin.infra.uitl.DocumentUtils;
import com.jy.rhin.sdk.dto.document.DocumentValidationFileDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;

/**
 *
 */
@Service
public class DocumentValidationService {

//    @Autowired
//    private UploadAPI uploadAPI;

    @Autowired
    private DocumentValidationFileDao documentValidationDao;

    public void importValidationFile(@RequestParam("ossUrl") String ossUrl) throws Exception {
//        String aliNetwork = uploadAPI.getAliNetwork(ossUrl);
//        InputStream inputStream = DocumentUtils.getInputStream(aliNetwork);
        InputStream inputStream = DocumentUtils.getInputStream(ossUrl);
        InputStreamReader isr = new InputStreamReader(inputStream);
        BufferedReader br = new BufferedReader(isr);
        StringBuffer neiRong = new StringBuffer();
        String line;
        String titleName = "";
        String titleType = "";
        int num = 0;
        // 逐行读取InputStream对象中的内容，并输出到控制台
        while ((line = br.readLine()) != null) {
            neiRong.append(line);
            if(line.contains("sch:title")){
                int startIndex = line.indexOf(">") + 1;
                int endIndex = line.lastIndexOf("<");
                String title = line.substring(startIndex, endIndex);
//                titleName = title.split("：")[1];
//                titleType = titleName.replaceAll("校验文件","");
                titleName = title;
                titleType = title.split("-")[0];
            }
            if(line.contains("<sch:rule")){
                num++;
            }
        }
        DocumentValidationFilePo documentValidationFilePo = new DocumentValidationFilePo();
        documentValidationFilePo.setFileName(titleName);
        documentValidationFilePo.setFileType(titleType);
        documentValidationFilePo.setRuleNum(num);
        documentValidationFilePo.setFileContent(neiRong.toString());
        documentValidationFilePo.setState("1");
        documentValidationFilePo.setUpdateTime(new Date());
        documentValidationFilePo.setUploadTime(new Date());
        documentValidationDao.save(documentValidationFilePo);
    }


   public void updateValidationFile(DocumentValidationFileDto dto){
       DocumentValidationFilePo documentValidationFilePo = new DocumentValidationFilePo();
       documentValidationFilePo.setId(dto.getId());
       if(!StringUtil.isNullOrWhiteSpace(dto.getState())){
           documentValidationFilePo.setState(dto.getState());
       }
       if(!StringUtil.isNullOrWhiteSpace(dto.getFileName())){
           documentValidationFilePo.setFileName(dto.getFileName());
       }
       if(!StringUtil.isNullOrWhiteSpace(dto.getFileType())){
           documentValidationFilePo.setState(dto.getState());
       }
       documentValidationDao.update(documentValidationFilePo);
    }

}
